import React from 'react';
import { Button, Modal,Spin } from 'antd';
import { WeaTab,WeaLocaleProvider,WeaDialog,WeaButtonIcon,WeaAuth,WeaRightMenu} from 'ecCom';
import { inject, observer } from 'mobx-react';
import { WeaTableNew } from "comsMobx";
import * as Util from '../../../util/Util'
const getLabel = WeaLocaleProvider.getLabel;
const toJS = mobx.toJS;
const WeaTable = WeaTableNew.WeaTable;
/**
 * 导出权限设置
 */
@inject('authStore')
@observer
export default class AuthIndex extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            selectedRowKeys:[],
            _height:Util.getCurrentHeight(),
        }
        this.scrollHeight = {y:this.state._height*0.5};
        this.rowSelection = {selectedRowKeys: this.state.selectedRowKeys,
            onChange: this.onTableSelectChange}
    }
    componentWillMount() {//一些初始化请求
        const{authStore,type} = this.props;
        authStore.doInit(type);
    }
    render() {
        const{searchAdForShareTable,shareBaseDataType,setObjShow,
            tableStore,getTableDatas,operatePermissions,loading} = this.props.authStore;
        const { form, show, conditioninfo} = searchAdForShareTable;
        const { showObj, objCondition } = toJS(shareBaseDataType);
        const {selectedRowKeys,_height} = this.state;
        return (
            <div>
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@th8317`} spinning={loading}>
                <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@5c3v7s`}
                    keyParam="viewcondition"
                    countParam="groupid"
                    searchType={["advanced"]}
                    buttons={[
                        <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@ffu8p9@adddata`} buttonType="add" type="primary" onClick={() =>  setObjShow(true)} />,
                        <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@5rc31h@deldata`} style={{marginRight:'15px'}} disabled={selectedRowKeys.length == 0}  onClick={() => this.delData()} buttonType="del"  type="primary" />
                      ]}
                    searchsAd={Util.getSearchs(form, conditioninfo,2,'','',this.props.ecId || '',"authindex")}
                    showSearchAd={show}
                    buttonsAd={this.getTabButtonsAd(this.props.ecId || '')}
                    setShowSearchAd={() => (searchAdForShareTable.show = !show)}
                    hideSearchAd={() => (searchAdForShareTable.show = false)}
                    searchsBaseValue={
                    (searchAdForShareTable.form.isFormInit &&
                        searchAdForShareTable.form.getFormParams() &&
                        searchAdForShareTable.form.getFormParams().content) ||""
                    }
                    advanceHeight="150"
                    onSearch={() => {getTableDatas();}}
                    onSearchChange={value => {form.updateFields({ content: value });}}/>
                <div style={{height:_height*0.5}}>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@4x8abf`} datas={this.getMenu()} showUrlItem={false}>
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@6eyt80`}
                            comsWeaTableStore={tableStore}
                            rowSelection={this.rowSelection}
                            hasOrder = {true}
                            needScroll = {true}
                            scroll={this.scrollHeight}
                            onOperatesClick={this.onOperatesClick}/>
                    </WeaRightMenu>
                </div>
                {showObj &&<WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@ya5ers`} icon="icon-coms-implement"
                iconBgcolor='rgb(150, 53, 138)'
                title={getLabel(611,"添加")}
                visible={showObj}
                conditions={objCondition}
                onOk={data => {
                    operatePermissions({operate:"add",...data});
                }}
                onCancel={() => setObjShow(false)}
                />}
                </Spin>
            </div>
        )
    }
    onTableSelectChange = (selectedRowKeys, selectedRows) => {
        this.setState ({selectedRowKeys: selectedRowKeys});
    };
    delData(){
        const {selectedRowKeys} = this.state;
        if(selectedRowKeys.length>0){
            this.batchDel(selectedRowKeys);
        }else{
            Modal.warning({
                title: getLabel(558,'提示'),
                content: getLabel(501159, '请选择要操作的数据')
            });
        }
    }
    batchDel(ids) {
        const {operatePermissions} = this.props.authStore;
        let _this = this;
        Modal.confirm({
          title: getLabel(558,'提示') ,
          content: getLabel(513503, '确认要删除选中记录吗？'),
          onOk() {
            operatePermissions({operate:"del",ids:ids});
            _this.setState({selectedRowKeys: []});
          },
          onCancel() {}
        });
    }
    getTabButtonsAd(ecId) {
        const {searchAdForShareTable,getTableDatas} = this.props.authStore;
        const { form } = searchAdForShareTable;
        return [
            <Button ecId={`${ecId}_Button@ag2y83@search`} type="primary"
                onClick={() => {getTableDatas();searchAdForShareTable.show = false;}}>
                {getLabel(197, "搜索")}</Button>,
            <Button ecId={`${ecId}_Button@151p23@reset`} type="ghost" onClick={() => form.reset()}>
                {getLabel(2022, "重置")}
            </Button>,
            <Button ecId={`${ecId}_Button@dxom16@cancle`} type="ghost" onClick={() => (searchAdForShareTable.show = false)}>
                {getLabel(31129, "取消")}
            </Button>
        ];
    }

    getMenu() {
        const {setObjShow,getTableDatas} = this.props.authStore;
        const {selectedRowKeys} = this.state;
        let menus = [];
        menus.push({
            key: '0',
            icon: <i className='icon-coms-search' />,
            content: getLabel(197, "搜索"),
            onClick: () => getTableDatas()
        });
        menus.push({
            key: '1',
            icon: <i className='icon-coms-tianjia' />,
            content: getLabel(611, '添加'),
            onClick: () => setObjShow(true)
        });
        menus.push({
            key: '2',
            icon: <i className="icon-coms-Batch-delete" />,
            content: getLabel(32136, '批量删除'),
            disabled:selectedRowKeys.length==0,
            onClick:() =>this.delData()
        });
    return menus;
    };
    //table操作
    onOperatesClick = (record, index, operate, flag) => {
        if (operate.index === '0') {
            record.operation='delete';
            this.batchDel(record.id)
        }
    }
};